package com.dataProcess;

import java.util.Date;
import java.util.List;

import com.enetiy.Area;
import com.enetiy.Areainfo;
import com.enetiy.Binding;
import com.enetiy.Userinfo;
import com.operation.NET;
import com.service.AreainfoService;
import com.service.AreausbleService;
import com.service.BindingService;
import com.service.ComboService;
import com.service.DealService;
import com.service.MapService;
import com.service.NetLinkService;
import com.service.UserinfoService;
import com.util.Log4gUtil;
import com.util.NetTimeUtil;

public class NetConnect {
	public int freeTime;
	public int freeIden;
	
	public int erweimaHimt;
	public int firstfreeHimt;
	public int comboHimt;
	public int bandingHimt;
	public String bandinglongTime="";
	
	public int vipHimt;
	public int vipid;
	public int comboid;
	public List<Integer> way; 
	
	
	public int  connect = 2;//网络连接状态 , 默认2
		public NetConnect() {
		super();
	}

		public NetConnect(int connect) {
			super();
			this.connect = connect;
		}

		public NetConnect(int freeTime, int erweimaHimt, int firstfreeHimt,
			int comboHimt, int bandingHimt) {
		super();
		this.freeTime = freeTime;
		this.erweimaHimt = erweimaHimt;
		this.firstfreeHimt = firstfreeHimt;
		this.comboHimt = comboHimt;
		this.bandingHimt = bandingHimt;
	}

		public void getNetConnect(Userinfo userinfo, Area siteArea, String ip,  String gateIp,  String mac,  String account,int deviceType,
				ComboService comboService,  AreainfoService areainfoService,  BindingService bindingService,  AreausbleService areausbleService
				,UserinfoService userinfoService, DealService dealService, String callid, String token,
				String apMac,MapService mapService,NetLinkService netLinkService) throws Exception{
			Log4gUtil.Info("getNetConnect-直接连网", userinfo.getUsers().getId()+"", "进入连网param&area:"+siteArea.getId(), "", "", token);
			NET nets = new NET();
			if(userinfo.getArea()!=null&&siteArea.getId()==userinfo.getArea().getId()){//为一个区域
				userinfo.setArea(userinfo.getArea());//用户登录区域修改
				Binding rBinding = null;//上次登录使用绑定
				//如上次登录网络为所记录的绑定    并绑定可用
				if((rBinding=userinfo.getBinding())!= null){
					//如是深职院学生连网
					if(siteArea.getId()==2&&rBinding.getTypeExplain().equals("学生")){
						connect  = nets.netAllow( "","",siteArea.getId(),ip,rBinding.getAreainfo().getAreatype().getBanfwidth(),
								siteArea.getNetOnline(),NetTimeUtil.getTimeUtil(new Date(),rBinding.getAreainfo().getValidTime()),
								gateIp,userinfo.getUsers().getId(),mac,account,
								2,deviceType,callid,token,apMac,"",mapService,areausbleService,netLinkService);
						bandinglongTime = rBinding.getAreainfo().getValidTime()+"";
					}else{
						connect  = nets.netAllow( "","",siteArea.getId(),ip,rBinding.getAreainfo().getAreatype().getBanfwidth(),
								rBinding.getArea().getNetOnline(),"",gateIp,userinfo.getUsers().getId(),mac,account,
								3,deviceType,callid,token,apMac,"",mapService,areausbleService,netLinkService);
					}
					if(connect==1){
						userinfo.setAreainfo(null);
						userinfo.setComboinfo(null);
						userinfo.setVipmember(null);
						userinfo.setNetOnline(1);
						userinfo.setArea(siteArea);
						userinfo.setUserIp(ip);
						userinfo.setUserMac(mac);
						Log4gUtil.Info("getNetConnect", userinfo.getUsers().getId()+"", "使用上次记录绑定账号", rBinding.getAccount(), callid, token);
					}
				}
			}	
				/**
				 * 与上次不同区域用户登录 或上次登录网络记录不可用
				 */
				//直接连网
			if(siteArea.getId()==20002){
				connect  = nets.netAllow( "","",siteArea.getId(),ip,6,
						siteArea.getNetOnline(),"",gateIp,userinfo.getUsers().getId(),mac,account,
						3,deviceType,callid,token,apMac,"",mapService,areausbleService,netLinkService);
				if(connect==1){
					userinfo.setNetOnline(1);
					userinfo.setArea(siteArea);
					userinfo.setUserIp(ip);
					userinfo.setUserMac(mac);
				}
			}
			
			
			
				int band = 0;//带宽大小
				int type = 0;
				//区域与上次联网区域不同,或者 以上操作没有连到网
				int userid = userinfo.getUsers().getId();
				if(connect==2){//如连过网 
					VerifyResou verify = new VerifyResou();
					Binding verifyBinding = null;
					verify.areainfos = areainfoService.selectAreainfo(siteArea.getId());
					way = verify.verifyWay(userinfo, comboService, dealService, siteArea.getId(),deviceType);
					
					Log4gUtil.Info("getNetConnect", userid+"", "",
							"netConnect.vipHimt:"+verify.vipUsable, "", token);
					
					//查询绑定权限
					verifyBinding = verify.verifyBinding(userid, siteArea.getId(), areausbleService, bindingService);
					if(verifyBinding!=null){
						band = verifyBinding.getAreainfo().getAreatype().getBanfwidth();
						type = 1;
						if(way.isEmpty()){
							for (int i = 0; i < verify.areainfos.size(); i++) {
								way.add(verify.areainfos.get(i).getAreanet().getId());
							}
						}
					}else if(verify.comboinfo!=null){
						type = 3;
					}else{
						type = 2;
					}
				
					if(type == 1){//使用绑定权限
						//如是深职院学生连网
						if(siteArea.getId()==2&&verifyBinding.getTypeExplain().equals("学生")){
							connect  = nets.netAllow( "","",siteArea.getId(),ip,verifyBinding.getAreainfo().getAreatype().getBanfwidth(),
									siteArea.getNetOnline(),NetTimeUtil.getTimeUtil(new Date(),verifyBinding.getAreainfo().getValidTime()),
									gateIp,userinfo.getUsers().getId(),mac,account,
									2,deviceType,callid,token,apMac,"",mapService,areausbleService,netLinkService);
							bandinglongTime = verifyBinding.getAreainfo().getValidTime()+"";
						}else{
							connect  = nets.netAllow( "","",siteArea.getId(),ip,verifyBinding.getAreainfo().getAreatype().getBanfwidth(),
									siteArea.getNetOnline(),"",gateIp,userinfo.getUsers().getId(),mac,account,
									3,deviceType,callid,token,apMac,"",mapService,areausbleService,netLinkService);
						}
						
						if(connect == 1){//连网成功
							userinfo.setNetOnline(1);
							userinfo.setBinding(verifyBinding);
							userinfo.setArea(siteArea);
							userinfo.setUserIp(ip);
							userinfo.setUserMac(mac);
							Log4gUtil.Info("getNetConnect", userinfo.getUsers().getId()+"", "查询使用绑定账号", verifyBinding.getAccount(), callid, token);
						}
					}else if (type == 2){//使用vip权限
						Log4gUtil.Info("getNetConnect", userid+"", "使用vip权限",
								"netConnect.vipHimt:"+verify.vipUsable, "", token);
						
						if(verify.vipKeeptime!=null&&!verify.vipKeeptime.getKeepTime().equals("")){
							
							connect  = nets.netAllow( "","",siteArea.getId(),ip,verify.vipmember.getSpeed(),
									siteArea.getNetOnline(),verify.vipKeeptime.getKeepTime(),gateIp,userinfo.getUsers().getId(),mac,account,
									5,deviceType,callid,token,apMac,verify.vipKeeptime.getNetOrder(),mapService,areausbleService,netLinkService);
							if(connect==1){//连网成功
								userinfo.setNetOnline(1);
								userinfo.setUserIp(ip);
								userinfo.setUserMac(mac);
								userinfo.setArea(siteArea);
								userinfo.setVipmember(verify.vipmember);
								userinfo.setAreainfo(null);
								userinfo.setBinding(null);
								userinfo.setComboinfo(null);
								Log4gUtil.Info("getNetConnect", userinfo.getUsers().getId()+"", "查询使用vip网络时间", verify.vipKeeptime.getKeepTime(), callid, token);
							}
						}else{
							vipHimt=verify.vipUsable;
						}
					}else if (type == 3){
						if(verify.comboinfo.getStatus()==1){
							connect = nets.netAllow( "","",siteArea.getId(),ip,verify.comboinfo.getBandwidth(),
									siteArea.getNetOnline(),null,gateIp,userinfo.getUsers().getId(),mac,account,
									6,deviceType,callid,token,apMac,"",mapService,areausbleService,netLinkService);
							if(connect==1){//连网成功
								userinfo.setNetOnline(1);
								userinfo.setUserIp(ip);
								userinfo.setUserMac(mac);
								userinfo.setArea(siteArea);
								userinfo.setVipmember(null);
								userinfo.setAreainfo(null);
								userinfo.setBinding(null);
								userinfo.setComboinfo(verify.comboinfo);
								Log4gUtil.Info("getNetConnect", userinfo.getUsers().getId()+"", "查询使用套餐", "", callid, token);
							}
						}else{
							comboid = verify.comboinfo.getComboid();
						}
					}else{//绑定和vip权限都没有
						//免费
						Areainfo verifyFree = verify.verifyFree(userid, siteArea.getId(), userinfoService, areainfoService,dealService, gateIp);
						if(verifyFree!=null){
							if(verify.freekeeptime!=null&&!verify.freekeeptime.equals("")){
								connect  = nets.netAllow( "","",siteArea.getId(),ip,verifyFree.getAreatype().getBanfwidth(),
										siteArea.getNetOnline(),verify.freekeeptime,gateIp,userinfo.getUsers().getId(),mac,account,
										1,deviceType,callid,token,apMac,"",mapService,areausbleService,netLinkService);
								if(connect==1){//连网成功
									userinfo.setNetOnline(1);
									userinfo.setUserIp(ip);
									userinfo.setUserMac(mac);
									Log4gUtil.Info("getNetConnect", userinfo.getUsers().getId()+"", "查询使用免费网络时间", verify.freekeeptime, callid, token);
								}
							}
						}
					}
				
					if(connect==2){//用户暂无权限
						Areainfo verifyQR = verify.verifyQR( siteArea.getId(), areainfoService);
						if(verifyQR!=null){
							erweimaHimt = 1;
						}
						
						bandingHimt = 	verify.bandingUsable;
						firstfreeHimt = verify.freeUsable;
						freeIden = verify.freeIden;
						freeTime = verify.freeTime;
					}
				}								
				userinfoService.updateUserinfo(userinfo);	
	}
		
}
